package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.pet.dao.PetLostCollectDao;
import io.renren.modules.pet.dto.PetLostCollectDTO;
import io.renren.modules.pet.entity.PetLostCollectEntity;
import io.renren.modules.pet.service.PetLostCollectService;
import io.renren.modules.pet.vo.PetLostCollectVO;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 宠物收藏表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-11
 */
@Service
public class PetLostCollectServiceImpl extends CrudServiceImpl<PetLostCollectDao, PetLostCollectEntity, PetLostCollectVO> implements PetLostCollectService {

    @Override
    public QueryWrapper<PetLostCollectEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(PetLostCollectDTO petCollectDTO) {
        PetLostCollectEntity petCollectEntity = ConvertUtils.sourceToTarget(petCollectDTO, PetLostCollectEntity.class);
        this.insert(petCollectEntity);
    }

    @Override
    public void update(PetLostCollectDTO petCollectDTO) {
        PetLostCollectEntity entity = ConvertUtils.sourceToTarget(petCollectDTO, PetLostCollectEntity.class);
        this.updateById(entity);
    }

    @Override
    public List<PetLostCollectVO> getByPetLostId(Long petLostId) {
        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        List<PetLostCollectEntity> entity = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entity, PetLostCollectVO.class);
    }

    @Override
    public PetLostCollectVO getByUserPetLostId(Long userId, Long petLostId) {
        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        wrapper.eq("creator", userId);
        PetLostCollectEntity entity = this.baseDao.selectOne(wrapper);
        return ConvertUtils.sourceToTarget(entity, PetLostCollectVO.class);
    }

    @Override
    public void cancelCollectPetLost(PetLostCollectDTO dto) {
        Long petLostId = dto.getPetLostId();
        // 获取当前用户信息
        Long userId = SecurityUser.getUserId();
        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        wrapper.eq("creator", userId);
        this.baseDao.delete(wrapper);
    }

    @Override
    public List<PetLostCollectVO> getByUserId(Long userId) {
        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("creator", userId);
        List<PetLostCollectEntity> entity = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entity, PetLostCollectVO.class);
    }

    @Override
    public void delByPetLostId(Long petLostId) {
        QueryWrapper<PetLostCollectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_lost_id", petLostId);
        this.baseDao.delete(wrapper);
    }

}